#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
"""
@author:apple
@file:demo02.py
@time:2022/03/09
"""
import requests
from lxml import etree

base_url = "https://movie.douban.com/top250"

headers = {
    "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}



def read_url(url):
    ...
    response = requests.get(base_url,headers=headers)
    return response.text


def parase(content):
    ...
    html = etree.HTML(content)
    items = html.xpath("//*[@id=\"content\"]/div/div[1]/ol/li/div")
    print(items)

    for item in items:
        ...
        # //*[@id="content"]/div/div[1]/ol/li[1]/div/div[1]/a/img
        icon_image = "".join(item.xpath("./div[1]/a/img/@src"))
        # //*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[1]/a/span[1]
        title = "".join(item.xpath("./div[2]/div[1]/a/span[1]/text()"))
        # //*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[2]/p[1]
        subject = "".join(item.xpath("./div[2]/div[2]/p[1]/text()")).split("\n")
        # //*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[2]/div/span[2]
        star = "".join(item.xpath("./div[2]/div[2]/div/span[2]/text()"))
        #//*[@id="content"]/div/div[1]/ol/li[2]/div/div[2]/div[2]/p[2]/span
        comment = "".join(item.xpath("./div[2]/div[2]/p[2]/span/text()"))
        print(icon_image,title, subject,star,comment)


def main():
    ...
    content = read_url(base_url)
    parase(content)


if __name__ == '__main__':
    pass
    main()